<?php
class MediaController extends Blog_Controller_AdminController {
	public function init() {
		parent::init();
		
		if (1 == $this->getParam('embed')) {
			$this->_helper->layout()->disableLayout();
		}
	}
	
	public function indexAction() {
		$this->forward('upload');
	}
	
	public function uploadAction() {
		$Form = new Zend_Form(array('name' => 'upload'));
		$Form->setEnctype(Zend_Form::ENCTYPE_MULTIPART)
			->addElements(array(
			new Zend_Form_Element_File('Files', array('multiple' => 'multiple', 'isArray' => TRUE, 'valueDisabled' => TRUE)),
			new Zend_Form_Element_Submit('submit', 'Tải lên')
			));
		
		if ($this->getRequest()->isPost()) {
			$Form->isValid($this->getRequest()->getPost());
			$Upload = new Zend_File_Transfer_Adapter_Http();
			$Upload->setDestination(ROOT_PATH . '/data/upload/');
			
			if ($Upload->isUploaded('Files')) {
				if ($Upload->receive('Files')) {
					$FileInfo = $Upload->getFileInfo('Files');
					///Update to the db
					$Media = new Admin_Model_Media_DbTable('Db');
					
					foreach ($FileInfo as $Info) {
						$Media->insert(array(
							'FileName' => $Info['name'],
							'Meta' => Zend_Serializer::serialize(array('path' => str_replace(ROOT_PATH, '', $Info['destination'] . '/'))),
							'FileType' => $Info['type'],
							'Created' => time()
						));
					}
				}
			}
		}
		
		$this->view->assign('form', $Form);
	}
	
	public function listAction() {
		//$Media = new Admin_Model_Media_Media();
		//$Media = new Admin_Model_Media_DbTable('Db');
		$Mapper = new Admin_Model_Media_Mapper();
		$Media = $Mapper->fetchAll();
		
		$this->view->assign('Items', $Media);
	}
}